<?php

namespace app\admin\controller;


use app\admin\model\RoomModel;
use app\admin\model\SchoolModel;
use app\admin\model\StudentModel;
use cmf\controller\AdminBaseController;
use think\Db;
use think\db\Query;

class StudentController extends AdminBaseController
{
    public function index()
    {
        $name = $this->request->param('name');
        $school_id = $this->request->param('school_id');
        $room_id = $this->request->param('room_id');
        $school=Db::name('school')->select();
        $room=Db::name('room')
            ->where('school_id', $school_id)
            ->select();;
        $data = Db::name('student s')
            ->where(function (Query $query) use($name,$school_id,$room_id){
                if ($name){
                    $query->where('s.name','like',"%$name%");
                }
                if ($school_id) {
                    $query->where('school_id', 'eq', $school_id );
                }
                if ($room_id) {
                    $query->where('room_id', 'eq', $room_id );
                }
            })
            ->join('ji_room room','room_id=room.id','LEFT')
            ->join('ji_school school','room.school_id=school.id','LEFT')
            ->join('ji_area a','a.areaId = s.province')
            ->join('ji_area b','b.areaId = s.city')
            ->join('ji_area c','c.areaId = s.area')
            ->field('s.*')
            ->field('school.name as school_name ')
            ->field('room.name as room_name')
            ->field('a.areaName as a')
            ->field('b.areaName as b')
            ->field('c.areaName as c')
            ->order('id desc')
            ->paginate(3,false,$this->request->param());
        $data->appends(['name'=>$name,'school_id'=>$school_id]);
        //分页样式
        $page = $data->render();
        //渲染显示页面，接收返回的值
        return view('',['student'=>$data,'page'=>$page, 'room_id' =>$room_id, 'room' => $room,'school_id'=>$school_id,'school'=>$school]);
    }
    public function add()
    {
        $id=input('id');
        $student=StudentModel::get($id);
        $school=SchoolModel::select();
        $room =RoomModel::all();
        $province = Db::name('area')->where('level',1)->select();
        return view('',['student'=>$student,'school'=>$school,'room'=>$room,'province' => $province,]);
    }
    public function addPost()
    {
        $data = input();
        $data['create_time'] = date('Y-m-d H:i:s',time());
        unset($data['photo_name']);
        $add=Db::name('student')->insert($data);
        if ($add){
            $this->success('添加成功','student/index');
        }else{
            $this->error('添加失败');
        }
    }
    public function getCity()
    {
        $areaId = input('areaId');
        $city = Db::name('area')->where('parentId',$areaId)->where('level',2)->select();
        $cityArr = ["<option value=''>请选择城市</option>"];
        foreach ($city as $v){
            $opt = "<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($cityArr,$opt);
        }
        //返回$data
        return $cityArr;
    }
    public function getArea()
    {
        $cityId = input('areaId');
        $area = Db::name('area')->where('parentId',$cityId)->where('level',3)->select();
        $cityArr = ["<option value=''>请选择县区</option>"];
        foreach ($area as $v){
            $opt = "<option value='".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($cityArr,$opt);
        }
        return $cityArr;

    }
    public function getRoomBySchool()
    {
        $schoolId = input('school_id');
        $rooms = RoomModel::where('school_id',$schoolId)->select();
        $num = count($rooms);
        if ($num > 0) {
            $data=["<option value=''>请选择班级</option>"];
            foreach ($rooms as $v){
                $opt = "<option value='".$v->id."'>".$v->name."</option>";
                array_push($data,$opt);
            }
            return $data;
        }else{
            return"<option value=''>暂无数据</option>";
        }
    }
    public function edit()
    {
        $id=input('id');
        $student = StudentModel::get($id);
        $school=SchoolModel::all();
        $schoolId=RoomModel::where('id',$student->room_id)->value('school_id');
        $room =RoomModel::where('school_id',$schoolId)->select();
        $province = Db::name('area')->where('level',1)->all();
        $city = Db::name('area')->where('level',2)->where('parentId',$student['province'])->all();
        $area = Db::name('area')->where('level',3)->where('parentId',$student['city'])->all();
        return view ('edit',[
            'student'=>$student,
            'school_id'=>$schoolId,
            'school'=>$school,
            'room'=>$room,
            'province' => $province,
            'city' => $city,
            'area' => $area,
        ]);
    }
    public function editPost()
    {
        $data = input();
        $data['update_time'] = date('Y-m-d H:i:s',time());
        unset($data['photo_name']);
        $edit = StudentModel::update($data, ['id' => $data['id']]);
        if ($edit){
            $this->success('修改成功','student/index');
        }else{
            $this->error('修改失败');
        }
    }
    public function delete()
    {
        $id=input('id');
        $del=StudentModel::destroy($id);
        if ($del){
            $this->success("删除成功！", url("student/index"));
        }else{
            $this->error("删除失败！");
        }
    }
    public function delAll()
    {
        $ids = input('ids/a');
        $del = StudentModel::destroy($ids);
        if($del){
            $this->success("删除成功！", url("student/index"));
        }else{
            $this->error("删除失败！");
        }
    }
    public function grade()
    {
        $id=input('id');
        $student = Db::name('student')
            ->alias('stu')
            ->join('grade g','stu.id = g.student_id','left')
            ->join('course c','c.id = g.course_id')
            ->join('room r','r.id = stu.room_id')
            ->join('school s','s.id = r.school_id')
            ->field('s.*')
            ->field('g.student_id,g.name grade_name,r.name room_name,stu.name student_name,c.name course_name,s.name school_name')
            ->where(['stu.id'=>$id])
            ->select();
        return $this->fetch('',['student'=>$student]);
    }

}